package example.service;

import example.dao.connect;
import example.pojo.user;
import org.json.JSONException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.*;

public class login_check {

    public static boolean check(String name,String password) throws SQLException
    {
        //处理业务逻辑
        try
        {
            Connection conn = connect.getConnection();
            PreparedStatement p = conn.prepareStatement("select * from login where username = ? and password = ?");
            p.setString(1, name);
            p.setString(2, password);
            ResultSet rs = p.executeQuery();
            while(rs.next())
            {
                connect.close(rs, p, conn);
                return true;
            }
            connect.close(rs, p, conn);

        }
        catch(SQLException e)
        {
            e.printStackTrace();
            System.out.println("数据库连接出错");
        }
        return false;
    }

    public static String logsession(HttpServletRequest request) {
        HttpSession uuu = request.getSession();
        user user1 = (user) uuu.getAttribute("username");
        if ( user1 != null )
        {
            uuu.setMaxInactiveInterval(1000);
            return user1.username;
        }
        return "2";
    }

    public static String logshenfen( String username ) throws SQLException, JSONException {
        String shenfen = null;
        Connection conn = connect.getConnection();
        Statement stmt = conn.createStatement();
        String sql = "select shenfen from login where username = '"+username+"'";
        ResultSet p = stmt.executeQuery(sql);
        while ( p.next() ){
            shenfen = p.getString("shenfen");
        }
        return shenfen;
    }

}
